package com.idbk.chargestation.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.user.ActivityMyMessageDetail;
import com.idbk.chargestation.bean.JsonMyMessage;
import com.idbk.chargestation.db.MessageDatabase;

import java.util.List;

/**
 * 消息列表适配器
 *
 * @author lupc, zhx
 */
public class MessageAdapter extends BaseAdapter implements OnItemClickListener {

    public final static String FROM_MESSAGE_MESSAGE_OBJ = "FROM_MESSAGE_MESSAGE_OBJ";
    private Context context;
    private LayoutInflater mInflater;
    private List<JsonMyMessage> data;
    private MessageDatabase messageDB;

    public MessageAdapter(
            Context context,
            ListView listView,
            List<JsonMyMessage> data,
            MessageDatabase messageDB) {
        this.context = context;

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        this.data = data;

        listView.setOnItemClickListener(this);

        this.messageDB = messageDB;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        final JsonMyMessage item = data.get(position);

        //1.判断 当前项目是否是未读项目，如果是未读项目，则设定其已读
        if (!item.isRead) {
            item.isRead = true;
            TextView tvMessageContent = (TextView) view.findViewById(R.id.item_message_title);
            tvMessageContent.setTextColor(ContextCompat
                    .getColorStateList(context, R.color.tj_text_content_9));
            messageDB.setMessageReaded(item.id, true);
        }

        //2.跳转 到消息详细界面
        Intent intent = new Intent(context, ActivityMyMessageDetail.class);
        intent.putExtra(FROM_MESSAGE_MESSAGE_OBJ, item);
        context.startActivity(intent);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        JsonMyMessage item = data.get(position);
        return item.id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = mInflater.inflate(R.layout.item_message, parent, false);
        } else {
            view = convertView;
        }

        TextView tvMessageDate = (TextView) view.findViewById(R.id.item_message_time);
        TextView tvMessageTitle = (TextView) view.findViewById(R.id.item_message_title);

        JsonMyMessage item = data.get(position);
        tvMessageTitle.setText(item.content);
        tvMessageDate.setText(item.date);
        if (item.isRead) {
            tvMessageTitle.setTextColor(ContextCompat.getColorStateList(context, R.color.tj_text_content_9));
        } else {
            tvMessageTitle.setTextColor(ContextCompat.getColorStateList(context, R.color.tj_text_title));
        }

        return view;
    }

}
